//获取应用实例
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadding: 1,
    addressRelationList: [],
    addressRelationAllList: [],
    addressRelationInspectList: [],
    addressRelationPatrolList: [],
    addressRelationDutyWeekList: [],
    checkboxItems: [{
        index: 0,
        value: '巡更',
        checked: true
      },
      {
        index: 1,
        value: '巡检',
        checked: true
      },
      {
        index: 2,
        value: '值周',
        checked: true
      },
    ]
  },
  onLoad: function (options) {
    this.refreshData()
  },

  refreshData() {
    wx.request({
      url: app.inspectUrl + '/WeChat/weChatSystem/getAddressRelation',
      method: 'GET',
      data: {
        schoolId: parseInt(app.globalData.departmentId)
      },
      success: res => {
        let addressRelationAllList = [];
        let addressRelationInspectList = [];
        let addressRelationPatrolList = [];
        let addressRelationDutyWeekList = [];
        for (let i = 0; i < res.data.length; i++) {
          let type;
          let macAddressIds = res.data[i].macAddress.split(":");
          if (res.data[i].type == 1) {
            type = "巡更"
            addressRelationAllList.push({
              id: res.data[i].id,
              address: res.data[i].address,
              macAddress: macAddressIds[1],
              type: type,
              typeNum: res.data[i].type
            })
            addressRelationPatrolList.push({
              id: res.data[i].id,
              address: res.data[i].address,
              macAddress: macAddressIds[1],
              type: type,
              typeNum: res.data[i].type
            })
          } else if (res.data[i].type == 2) {
            type = "巡检"
            addressRelationAllList.push({
              id: res.data[i].id,
              address: res.data[i].address,
              macAddress: macAddressIds[1],
              type: type,
              typeNum: res.data[i].type
            })
            addressRelationInspectList.push({
              id: res.data[i].id,
              address: res.data[i].address,
              macAddress: macAddressIds[1],
              type: type,
              typeNum: res.data[i].type
            })
          } else if (res.data[i].type == 3) {
            type = "值周"
            addressRelationAllList.push({
              id: res.data[i].id,
              address: res.data[i].address,
              macAddress: macAddressIds[1],
              type: type,
              typeNum: res.data[i].type
            })
            addressRelationDutyWeekList.push({
              id: res.data[i].id,
              address: res.data[i].address,
              macAddress: macAddressIds[1],
              type: type,
              typeNum: res.data[i].type
            })
          }

        }
        this.setData({
          addressRelationList: addressRelationAllList,
          addressRelationAllList: addressRelationAllList,
          addressRelationPatrolList: addressRelationPatrolList,
          addressRelationInspectList: addressRelationInspectList,
          addressRelationDutyWeekList: addressRelationDutyWeekList,
          loadding: 0
        })
      }
    })
  },

  showSelect: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(800).step()
    this.setData({
      animationSelect: animation.export(),
      showSelectStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationSelect: animation.export()
      })
    }.bind(this), 200)
  },
  // 隐藏遮罩层
  hideSelect: function (e) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(800).step()
    this.setData({
      animationSelect: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationSelect: animation.export(),
        showSelectStatus: false
      })
    }.bind(this), 200)
  },

  delRelation: function (e) {
    console.log("e", e)
    let that = this
    wx.showModal({
      title: '提示',
      content: '删除地点以及其关联关系，确定删除？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.inspectUrl + '/WeChat/weChatSystem/delAddressAndRelation', 
            method: 'POST',  
            dataType: 'json',
            data: { 
              schoolId : parseInt(app.globalData.departmentId) ,
              id: e.currentTarget.dataset.id,
              type: e.currentTarget.dataset.type
            },
            success: res => {
              if(res.data == 'success'){
                wx.showToast({
                  title: "删除成功"
                }) 
                that.refreshData()
              }else{
                wx.showToast({
                  title: "删除失败",
                  image: "/images/error.png"
                }) 
                wx.hideLoading()
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络异常！',
                mask: true,
                image: "/images/error.png"
              })
            }
          })
        } else {
          console.log('取消')
        }
      }
    })
  },


  typeChange: function (e) {
    console.log(e)
    let checkboxItems = this.data.checkboxItems
    let checkBoxList = e.detail.value
    for (var i = 0; i < checkboxItems.length; ++i) {
      checkboxItems[i].checked = false;
    }
    for (var i = 0; i < checkBoxList.length; ++i) {
      checkboxItems[checkBoxList[i]].checked = true;
    }
    this.setData({
      checkboxItems: checkboxItems
    })
  },
  confirmSelect: function (e) {
    let checkboxItems = this.data.checkboxItems
    let flag = 0;
    for (let i = 0; i < checkboxItems.length; i++) {
      if (checkboxItems[i].checked) {
        flag = 1;
        break;
      }
    }
    if (flag == 0) {
      wx.showToast({
        title: "请选择类型",
        image: "/images/warning.png"
      })
    } else {
      let addressRelationList = []
      if (checkboxItems[0].checked) {
        addressRelationList.push.apply(addressRelationList, this.data.addressRelationPatrolList)
      }
      if (checkboxItems[1].checked) {
        addressRelationList.push.apply(addressRelationList, this.data.addressRelationInspectList)
      }
      if (checkboxItems[2].checked) {
        addressRelationList.push.apply(addressRelationList, this.data.addressRelationDutyWeekList)
      }
      this.setData({
        addressRelationList: addressRelationList
      })
      wx.showToast({
        title: '搜索成功！',
        icon: 'none',
        duration: 1000
      })
      this.hideSelect();
    }
  }
})